package com.cedarhd.finance.bl.controller.api;

import com.cedarhd.finance.bl.common.JsonResult;
import com.cedarhd.finance.bl.common.ProcessAPI;
import com.cedarhd.finance.bl.common.enums.ResultCode;
import com.cedarhd.finance.bl.controller.Routes;
import com.cedarhd.finance.bl.domain.entity.Student;
import com.cedarhd.finance.bl.domain.repository.specification.StudentSpecification;
import com.cedarhd.finance.bl.domain.service.StudentService;
import com.cedarhd.finance.bl.dto.StudentDTO;
import com.cedarhd.finance.bl.dto.StudentQueryDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Evan
 * @date 2018/3/5
 */
@RestController
@Api(value = "hello", description = "hello")
public class HelloWorldController {

    @Autowired
    private StudentService studentService;

    private Logger logger = LoggerFactory.getLogger(HelloWorldController.class);


    @RequestMapping(value = Routes.HOME,method = RequestMethod.GET)
    @ApiOperation(value = "最简单的实例", notes = "最简单的实例", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult hello(HttpServletRequest request){
        ProcessAPI api = (jsonResult, validate) -> {
            logger.info("Hello World!");
            jsonResult.setResult("Hello World!");
        };
        return api.process(request);
    }

    /**
     *
     * @param request
     * @param req
     * @return
     */
    @RequestMapping(value = Routes.STUDENT_ADD,method = RequestMethod.POST)
    @ApiOperation(value = "新增学生", notes = "新增学生", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult add(HttpServletRequest request, @RequestBody StudentDTO req){
        ProcessAPI api = (result, validate) -> {
            Student student = studentService.add(req);
            result.setResult(student);
        };
        return api.process(request);
    }

    @RequestMapping(value = Routes.STUDENT_DETAIL,method = RequestMethod.GET)
    @ApiImplicitParam(name = "studentId", value = "学生ID", required = true,paramType = "query",dataType = "int")
    @ApiOperation(value = "查看学生详情", notes = "查看学生详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult detail(HttpServletRequest request, Integer studentId){
        ProcessAPI api = (result, validate) -> {
            //检查必填参数是否为空
            validate.check("studentId",studentId);
            Student student = studentService.getRepository().findOne(studentId);
            if(student == null){
                result.setMsg(ResultCode.NO_DATA.getMsg());
                result.setResultCode(ResultCode.NO_DATA.getCode());
                return;
            }

            result.setResult(student);

        };
        return api.process(request);
    }

    @RequestMapping(value = Routes.STUDENT_LIST,method = RequestMethod.GET)
    @ApiOperation(value = "学生列表", notes = "学生列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult list(HttpServletRequest request, StudentQueryDTO req){
        ProcessAPI api = (result, validate) -> {
            Specification<Student> specification = StudentSpecification.buildFromDTO(req);
            Page<Student> studentPage = studentService.getRepository().findAll(specification,req.getPage());
            List<Student> studentList = studentService.coverStudentDTO(studentPage);
            result.setResult(studentList);
            result.setSize(studentPage.getTotalElements());
        };
        return api.process(request);

    }

}
